package com.azt.provider.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.azt.api.pojo.FinanceApplyReq;
import com.azt.api.pojo.SmsParam;
import com.azt.api.service.MqProducerService;
import com.azt.api.service.MsgConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.FinanceApply;
import com.azt.api.service.FinanceApplyService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.FinanceApplyMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class FinanceApplyServiceImpl implements FinanceApplyService {

	@Autowired
	private FinanceApplyMapper financeApplyMapper;

	@Autowired
	private MsgConfigService msgConfigService;

	@Override
	public Pagination getFinanceApplyPage(int pageno, int pagesize, Map<String, String> searchMap) {

		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		PageHelper.orderBy("state asc, createtime desc");
		List<FinanceApply> list = financeApplyMapper.getFinanceApplyByParams(searchMap);
		Pagination p = new Pagination(startPage, list);
		p.setList(list);
		p.setParams(searchMap);
		return p;

	}

	@Override
	public void updateFinanceApply(FinanceApply apply) {
		financeApplyMapper.updateByPrimaryKey(apply);
	}

	@Override
	public FinanceApply getFinanceApplyByid(Integer id) {
		return financeApplyMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deleteFinanceApplyById(Integer id) {
		financeApplyMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateMemo(Integer id, String memo) {
		financeApplyMapper.updateMemo(id, memo);
	}

	@Override
	public void save(FinanceApplyReq req) {
		FinanceApply apply = new FinanceApply();
		apply.setState(0);
		BeanUtils.copyProperties(req, apply);
		financeApplyMapper.insert(apply);
		// 发送短信通知运营
		Map<String, String> params = new HashMap<>();
		params.put("companyName", StringUtils.defaultString(apply.getCompanyName()));
		params.put("contactName", apply.getContactName());
		params.put("contactTel", apply.getContactTel());
		msgConfigService.sendMessage("FINANCE_APPLY", params);
	}
}
